Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

题目大意:返回给定二叉树的最小深度

题目难度:Medium

/**
 * Created by gzdaijie on 16/6/9
 */
public class Solution {
    public int minDepth(TreeNode root) {
        return root == null ? 0 : helper(root, 1);
    }

    private int helper(TreeNode root, int depth) {
        if (root.left == null && root.right == null) return depth;

        int left = Integer.MAX_VALUE, right = Integer.MAX_VALUE;
        if (root.left != null) left = helper(root.left, depth + 1);
        if (root.right != null) right = helper(root.right, depth + 1);

        return Math.min(left, right);
    }
}
gzdaijie            updated 2016-06-09 15:26:44

results matching ""

    No results matching ""